// pages/msg/msg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:"",  // 索引
    msg:[] ,
    opindex:"",
    resize:"50rpx",
    reback:"#EBEBEB",
    recolor:"#701C12" // 文章数组
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.index);

    const msg = wx.getStorageSync('msg');
    console.log(msg);
    this.setData({
      msg,
      currentIndex:options.index,
      opindex:(parseInt(options.index)+1).toString()
    });
    console.log(opindex);
    console.log(this.data.msg);
    // this.data.msg.replace(/\\n/g, '\n')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  openActionsheet:function(){
    var self = this
    wx.showActionSheet({
      itemList: ['小','中','大'],
      itemColor:'#007aff',
     
      success(res){
          console.log(res)
          if(res.tapIndex===0){
            self.setData({
              resize:"30rpx"
            })
          }else if(res.tapIndex===1){
            self.setData({
              resize:"50rpx"
            })
          }else if(res.tapIndex===2){
            self.setData({
              resize:"70rpx"
            })
          }
      }
    })
  },
  changeActionsheet:function(){
    var self = this
    wx.showActionSheet({
      itemList: ['正常','护眼','夜间'],
      itemColor:'#007aff',
      success(res){
          console.log(res)
          if(res.tapIndex===0){
            self.setData({
              recolor:"black",
              reback:"white"
            })
          }else if(res.tapIndex===1){
            self.setData({
              recolor:"#701C12",
              reback:"#EBEBEB"
            })
          }else if(res.tapIndex===2){
            self.setData({
              recolor:"white",
              reback:"#383535"
            })
          }
      }
    })
  },

  btn(e){
    console.log(e.currentTarget.dataset.index);
    if(e.currentTarget.dataset.index==-1){
      if(this.data.currentIndex>0){
        let currentIndex = parseInt(this.data.currentIndex) + parseInt(e.currentTarget.dataset.index);
        let opindex = parseInt(this.data.opindex) +parseInt(e.currentTarget.dataset.index);

        this.setData({
          currentIndex,
          opindex
        });
        console.log(this.data.currentIndex)
        if (wx.pageScrollTo) {
          wx.pageScrollTo({
            scrollTop: 0
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
          })
        }
      }else{
        wx.showModal({
          title: '提示',
          content:"这已经是第一章了！",
          showCancel:false,
          success:()=>{
            return
          }
        })
      }
    }else if(e.currentTarget.dataset.index==1){
      if(this.data.currentIndex+1==this.data.msg.length){
        wx.showModal({
          title: '提示',
          content:"这已经是最后一章了！",
          showCancel:false,
          success:()=>{
            return
          }
        })
      }else{
        let currentIndex = parseInt(this.data.currentIndex) + parseInt(e.currentTarget.dataset.index);
        let opindex = parseInt(this.data.opindex) + parseInt(e.currentTarget.dataset.index);
        this.setData({
          currentIndex,
          opindex
        });
        console.log(this.data.currentIndex)
        if (wx.pageScrollTo) {
          wx.pageScrollTo({
            scrollTop: 0
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
          })
        }
      }
    }
  }

})